package com.ruoyi.websocket.init;

import com.ruoyi.websocket.domain.AutoReply;
import com.ruoyi.websocket.service.AutoReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class InitEsData {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    @Autowired
    private AutoReplyService autoReplyService;
    @PostConstruct
    public void InitEsData() {
        System.out.println("开始初始化es数据");
        //判断es索引库存不存在不存在则创建
        IndexOperations indexOps = elasticsearchTemplate.indexOps(AutoReply.class);
        if (!indexOps.exists()) {
            indexOps.create();
            Document mapping = indexOps.createMapping(AutoReply.class);
            indexOps.putMapping(mapping);
        }
        autoReplyService.list().forEach(autoReply -> {
            elasticsearchTemplate.save(autoReply);
        });

        System.out.println("结束初始化es数据");

    }
}
